use warnings;
use strict;  

use FindBin qw( $Bin );
use lib ("$Bin/lib", "$Bin/../lib");
use Data::Dumper;
  
use Test::More tests => 11;
use Log::Log4perl qw(get_logger);
use English qw( -no_match_vars );
use TestGearman;
use JSON::XS;
 
use Gearman::Client;
my $PORTT = 10221;

my $timeout = 0;
my ($ret, $job_handle);
my $FAILED =0;
my $test_req_data = encode_json { metaid => 6122, start => time() - 100000,  
                             end => time()  - 100000- 3600,  table => 'Pinger_data', 
			     resolution => 10  };
my $test_req_remote = encode_json { metaid => 6122, start => time() - 100000,  
                             end => time() - 100000 - 3600,  table => 'Pinger_data',
			     md_row => { url => 'http://frown.es.net:8075/perfSONAR_PS/services/pinger/ma',
			                 subject => '<nmwgt:subject id="subj5"><nmwgt:endPointPair><nmwgt:src value="frown.es.net" type="hostname"/><nmwgt:dst value="pinger.slac.stanford.edu" type="hostname"/></nmwgt:endPointPair></nmwgt:subject>'
			      },
			     type => 'pinger', 
			     resolution => 10 
			      };
my $test_snmp_remote = encode_json { 
                             metaid => 4231, start => time() - 100000,
                             end => time() - 100000 - 3600,  
			     table => 'Snmp_data',
			     url => 'http://ps3.es.net:8080/perfSONAR_PS/services/snmpMA',
			     type => 'snmp', 
			     resolution => 5,
			     snmp_ip => '134.55.221.26',
			     direction => 'in',
			      };

##start_server($PORTT);
##wait_for_port($PORTT);
#eval {
#   start_worker($PORTT,2)
#};
#isnt(defined $EVAL_ERROR,' trying to run data worker') or diag($EVAL_ERROR);
#undef $EVAL_ERROR;

# client
my $client=  Gearman::Client->new(exceptions => 1);
isa_ok($client, 'Gearman::Client');
$ret = $client->job_servers("localhost:$PORTT");
ok($ret, 'adding server');
 

# single   task interface
$ret =  $client->do_task("get_data" => $test_req_data);
like($$ret, qr/ok/, 'get_data task');

$ret=  $client->do_task("dispatch_data" =>  $test_req_remote);
like($$ret, qr/ok/, 'dispatch_data task'); 

$ret=  $client->do_task("get_remote_data" =>  $test_req_remote);
like($$ret, qr/ok/, 'get_remote_data task'); 

$ret = $client->do_task("get_remote_snmp" => $test_snmp_remote);
like($$ret, qr/ok/, 'get_remote_snmp task'); 

#  async task interface
my $task_set = $client->new_task_set;
$task_set->add_task( "get_data" => $test_req_data, 
 		    { on_fail =>  sub {$FAILED++}, 
 		      on_complete => sub {like(${$_[0]}, qr/ok/, 'get_data task 2');}});

$task_set->add_task( "dispatch_data" => $test_req_remote,
                    { on_fail =>  sub {$FAILED++} , 
 		      on_complete => sub {like(${$_[0]}, qr/ok/, 'dispatch_data task 2');}});		      

$task_set->add_task( "get_remote_data" => $test_req_remote,
                    { on_fail =>  sub {$FAILED++} , 
 		      on_complete => sub {like(${$_[0]}, qr/ok/, 'get_remote_data task 2');}});
		      
$task_set->add_task( "get_remote_snmp" =>  $test_snmp_remote, 
                    { on_fail =>  sub {$FAILED++}, 
 		      on_complete => sub {like(${$_[0]}, qr/ok/, 'get_remote_snmp task 2');}}); 
$ret = $task_set->wait();
is($FAILED, 0, 'on_fail not called on a successful result') or diag("these many failed=$FAILED");
 
